package com.ulife.order.schedule.tasks;

import com.ulife.common.gateway.utils.define.Version;
import com.ulife.common.gateway.utils.entity.ServiceException;
import com.ulife.order.api.OrderService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * Created by Ulife on 2016/1/15.
 */
@Component
@Configurable
@EnableScheduling
public class OrderTask {
    private static Lock cancelLock = new ReentrantLock();
    private static Lock splitLock = new ReentrantLock();
    private static Lock syncLock = new ReentrantLock();
    private static Lock pushLock = new ReentrantLock();
    Logger logger = LoggerFactory.getLogger(OrderTask.class);

    /*
    * 内网dubbo远程调用
    * */
    @Reference(version = Version.LATEST)
    OrderService orderService;

    /*
   * 拆分订单
   * */
    @Scheduled(cron = "${order.schedule.daliylimitcache}")
    public void daliylimitcache() {

        boolean rst = false;
        try {
            rst = orderService.DaliyLimitCache();
            logger.info("split result is " + String.valueOf(rst));
        } catch (ServiceException e) {
            logger.error(e.getMessage());
        }
    }

    /*
    * 拆分订单
    * */
    @Scheduled(cron = "${order.schedule.split}")
    public void split() {

        splitLock.lock();
        String rst = "";
        try {
            rst = orderService.split();
            logger.info("split result is " + String.valueOf(rst));
        } catch (ServiceException e) {
            logger.error(e.getMessage());
        }
        finally {
            splitLock.unlock();
        }
    }

    /*
    * 同步订单到om
    * */
    @Scheduled(cron = "${order.schedule.push}")
    public void push() {

        pushLock.lock();
        String rst = "";
        try {
            rst = orderService.push();
            logger.info("push Order result is " + String.valueOf(rst));
        } catch (ServiceException e) {
            logger.error(e.getMessage());
        }
        finally {
            pushLock.unlock();
        }
    }

    /*
    * 同步订单状态 && tracking info
    * */
    @Scheduled(cron = "${order.schedule.sync}")
    public void sync() {

        syncLock.lock();
        String rst = "";
        try {
            rst = orderService.sync();
            logger.info("sync result is " + String.valueOf(rst));
        } catch (ServiceException e) {
            logger.error(e.getMessage());
        }
        finally {
            syncLock.unlock();
        }
    }

    /*
    * 同步订单状态 && tracking info
    * */
    @Scheduled(cron = "${order.schedule.cancel}")
    public void autoCancel() {
        cancelLock.lock();

        String rst = "";
        try {
            rst = orderService.autoCancel();
            logger.info("auto Cancel result is " + String.valueOf(rst));
        } catch (ServiceException e) {
            logger.error(e.getMessage());
        }
        finally {
            cancelLock.unlock();
        }
    }
}
